package cn.damai.content.service.impl;

import cn.damai.content.mapper.HomeMapper;
import cn.damai.content.model.ItemTypeDTO;
import cn.damai.content.service.HomeService;
import cn.damai.model.Image;
import cn.damai.util.DubboUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页业务实现类
 */
@Component
@Service(version = DubboUtil.VRESION,group = DubboUtil.GROUP)
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeMapper homeMapper;

    @Override
    public Map<String, Object> listItemToHome() {
//        (1:演唱会-concert,4:话剧歌剧-drama,5:体育比赛-sports,8:亲子-family)
        Map<String,Object> map = new HashMap<>();
//        若空,添加默认图片
        Image image = new Image();
        image.setImgUrl("/damai/image/failPicture.jpg");

        List<ItemTypeDTO> event = homeMapper.firstList4Type();
        List<ItemTypeDTO> hotEvent = homeMapper.list4TypeHotEvent();

//        循环判断并分类存储
        for(int i = 0;i<event.size();i++){
            ItemTypeDTO itemTypeDTO = event.get(i);
            if (itemTypeDTO.getId() == 1){
                itemTypeDTO = isNullSetImage(itemTypeDTO, image);
                map.put("concert",itemTypeDTO);
            }else if(itemTypeDTO.getId() == 4){
                itemTypeDTO = isNullSetImage(itemTypeDTO, image);
                map.put("drama",itemTypeDTO);
            }else if(itemTypeDTO.getId() == 5){
                itemTypeDTO = isNullSetImage(itemTypeDTO, image);
                map.put("sports",itemTypeDTO);
            }else if(itemTypeDTO.getId() == 8){
                itemTypeDTO = isNullSetImage(itemTypeDTO, image);
                map.put("family",itemTypeDTO);
            }
        }

        for(int i = 0;i<hotEvent.size();i++){
            ItemTypeDTO itemTypeDTO = hotEvent.get(i);
            if (itemTypeDTO.getId() == 1){
                itemTypeDTO = isNullSetImage(itemTypeDTO, image);
                map.put("hotConcert",itemTypeDTO);
            }else if(itemTypeDTO.getId() == 4){
                itemTypeDTO = isNullSetImage(itemTypeDTO, image);
                map.put("hotDrama",itemTypeDTO);
            }else if(itemTypeDTO.getId() == 5){
                itemTypeDTO = isNullSetImage(itemTypeDTO, image);
                map.put("hotSports",itemTypeDTO);
            }else if(itemTypeDTO.getId() == 8){
                itemTypeDTO = isNullSetImage(itemTypeDTO, image);
                map.put("hotFamily",itemTypeDTO);
            }
        }

        return map;

    }

//  空图片添加方法封装
    private ItemTypeDTO isNullSetImage(ItemTypeDTO itemTypeDTO,Image image){
        if(itemTypeDTO.getItemDTOS() != null){
            for(int i = 0;i < itemTypeDTO.getItemDTOS().size();i++){
                if(itemTypeDTO.getItemDTOS().get(i).getImage() == null){
                    itemTypeDTO.getItemDTOS().get(i).setImage(image);
                }
            }
            return itemTypeDTO;
        }else {
            if(itemTypeDTO.getItemDTO().getImage() == null){
                itemTypeDTO.getItemDTO().setImage(image);
            }
            return itemTypeDTO;
        }
    }


}
